﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using ShopOnline.BLL;

namespace ShopOnline.Controls.BusinessCard
{
    public partial class Card : System.Web.UI.UserControl
    {
        private const string CLS_BORDER_RADIUS_ALL = "radius-all-corner";
        private const string CLS_BORDER_RADIUS_LEFTRIGHT_1 = "radius-leftright-corner-1";
        private const string CLS_BORDER_RADIUS_LEFTRIGHT_2 = "radius-leftright-corner-2";

        public int CardID
        {
            get
            {
                object o = ViewState["CardID"];
                return o == null ? 0 : Convert.ToInt32(o);
            }
            set
            {
                ViewState["CardID"] = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                object o = ViewState["ReadOnly"];
                return o == null ? false : Convert.ToBoolean(o);
            }
            set
            {
                ViewState["ReadOnly"] = value;
            }
        }

        public bool IsViewBackCard
        {
            get
            {
                object o = ViewState["IsViewBackCard"];
                return o == null ? false : Convert.ToBoolean(o);
            }
            set
            {
                ViewState["IsViewBackCard"] = value;
            }
        }

        public bool IsEmptyCard
        {
            get
            {
                object o = ViewState["IsEmptyCard"];
                return o == null ? true : Convert.ToBoolean(o);
            }
            set
            {
                ViewState["IsEmptyCard"] = value;
            }
        }

        public bool Display
        {
            get
            {
                object o = ViewState["Display"];
                return o == null ? true : Convert.ToBoolean(o);
            }
            set
            {
                ViewState["Display"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Refresh();
            }
        }

        public void Refresh()
        {
            if (!IsEmptyCard)
                RenderCard();
            else
                RenderCardEmpty();
        }

        private void RenderCard()
        {
            string sRender = string.Empty;
            BCCard card = BCCard.GetCardByID(CardID);
            if (card.RowCount > 0)
            {
                BCCardDetail cardDetail;
                BCCardDetail backCard = new BCCardDetail();
                BCCardDetail frontCard = new BCCardDetail();
                string cardType = "front";
                if (IsViewBackCard)
                {
                    cardDetail = BCCardDetail.GetCardDetailByID(card.BackCardID);
                    backCard = BCCardDetail.GetCardDetailByID(card.BackCardID);
                    cardType = "back";
                }
                else
                {
                    cardDetail = BCCardDetail.GetCardDetailByID(card.FrontCardID);
                    frontCard = BCCardDetail.GetCardDetailByID(card.FrontCardID);
                }

                if (cardDetail.RowCount > 0)
                {
                    #region == Border Style ==
                    string borderStyle = "";
                    if (card.BorderType == (int)BCCard.BorderStyle.RadiusAll)
                    {
                        borderStyle = CLS_BORDER_RADIUS_ALL;
                    }
                    else if (card.BorderType == (int)BCCard.BorderStyle.RadiusLeftRight1)
                    {
                        borderStyle = CLS_BORDER_RADIUS_LEFTRIGHT_1;
                    }
                    else if (card.BorderType == (int)BCCard.BorderStyle.RadiusLeftRight2)
                    {
                        borderStyle = CLS_BORDER_RADIUS_LEFTRIGHT_2;
                    }
                    #endregion

                    #region == Background Image
                    string urlBackgroundImage = string.Empty;
                    if (!string.IsNullOrEmpty(cardDetail.s_Background))
                    {
                        urlBackgroundImage = Utilities.GetKeySetting("TempThumbFolder") + "/" + cardDetail.s_Background;
                    }
                    #endregion

                    #region == Logo ==
                    string logoImage = string.Empty;
                    if (!string.IsNullOrEmpty(cardDetail.s_Logo))
                    {
                        string urlLogo = Utilities.GetKeySetting("TempThumbFolder") + "/" + cardDetail.s_Logo;
                        logoImage = string.Format("<img class=\"card-logo\" src=\"{0}\" />", urlLogo);
                    }
                    #endregion

                    #region == TextBlock ==
                    BCTextBlock bcTextBlock = BCTextBlock.GetTextBlockByCardDetailID(cardDetail.ID);
                    string textBlocks = string.Empty;
                    if (bcTextBlock.RowCount > 0)
                    {
                        for (int i = 0; i < bcTextBlock.RowCount; i++)
                        {
                            textBlocks += string.Format("<div class='text-block' style='left:{1}px;top:{2}px;'><div class='text'>{0}</div></div>",
                                bcTextBlock.s_Text,
                                bcTextBlock.s_PositionLeft,
                                bcTextBlock.s_PositionTop);
                            bcTextBlock.MoveNext();
                        }
                    }
                    #endregion

                    if (!ReadOnly)
                    {
                        sRender = string.Format("<div id='{1}-card' class='card {2}' style='background-image: url({3});display: {6};'><div id='body-{1}-card' class='body-card'>{4}{5}</div></div>",
                            cardType, borderStyle, urlBackgroundImage, logoImage, textBlocks, (Display) ? "block" : "none"
                            );
                    }
                    else
                    {
                        sRender = string.Format("<div class='card {1} readonly' style='background-image: url({2});display: {6}' cardid='{5}'><div class='body-card body-card-review' >{3}{4}</div></div>",
                            cardType, borderStyle, urlBackgroundImage, logoImage, textBlocks, card.s_ID, (Display) ? "block" : "none"
                            );
                    }
                }
            }
            lblCard.Text = sRender;
        }

        private void RenderCardEmpty()
        {
            string sRender = string.Empty;
            string cardType = "front";
            if (!ReadOnly && IsViewBackCard)
            {
                cardType = "back";
            }

            #region == Border Style ==
            string borderStyle = "";
            #endregion

            #region == Background Image ==
            string urlBackgroundImage = string.Empty;
            #endregion

            #region == Logo ==
            string logoImage = string.Empty;
            #endregion

            #region == TextBlock ==
            string textBlocks = string.Empty;
            #endregion

            if (!ReadOnly)
            {
                sRender = string.Format("<div id='{0}-card' class='card {1}' ><div id='body-{0}-card' class='body-card'>{3}{4}</div></div>",
                    cardType, borderStyle, urlBackgroundImage, logoImage, textBlocks
                    );
            }
            else
            {
                sRender = string.Format("<div class='card {1}' ><div class='body-card body-card-review' >{3}{4}</div></div>",
                    cardType, borderStyle, urlBackgroundImage, logoImage, textBlocks
                    );
            }
            lblCard.Text = sRender;
        }
    }

    ///// <summary>
    ///// Class that will represent Operation related event arguments
    ///// </summary>
    //public class CardEventArgs : EventArgs
    //{
    //    private int _cardID;

    //    public CardEventArgs(int cardID)
    //    {
    //        this._cardID = cardID;
    //    }

    //    /// <summary>
    //    /// Property to get/set CardID
    //    /// </summary>
    //    public int CardID
    //    {
    //        get
    //        {
    //            return this._cardID;
    //        }
    //        set
    //        {
    //            this._cardID = value;
    //        }
    //    }
    //}    
}